//
//  VC_CourseDetail.m
//  yjw-winner
//
//  Created by Kae on 2018/9/5.
//  Copyright © 2018年 Shanghai decade financial information service co., LTD. All rights reserved.
//

#import "VC_CourseDetail.h"
#import "VC_CourseSelectedWorks.h"


@interface VC_CourseDetail ()

@property (nonatomic, strong) UIButton *b1;
@property (nonatomic, strong) UIButton *b2;
@property (nonatomic, strong) UIViewController *currentViewController; // 选中的viewController
@property (nonatomic, strong) UIView *signView;
@property (nonatomic, strong) VC_StudyData *studyVC;
@property (nonatomic, strong) VC_CourseSelectedWorks *workVC;
@property (nonatomic, strong) UIView *showArea;

@property (nonatomic, copy) CallBackStudyDataBlock block;
@end

@implementation VC_CourseDetail
- (instancetype)initWith:(CallBackStudyDataBlock)block {
    if (self = [super init]) {
        self.block = block;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _b1 = [UIButton buttonWithType:UIButtonTypeCustom];
    _b1.frame = CGRectMake(0, 0, self.view.mj_w * 0.5, 40);
    
    [_b1 setTitle:@"学习资料" forState:UIControlStateNormal];
    [_b1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_b1 setTitleColor:MJRefreshColor(220, 188, 151) forState:UIControlStateSelected];
    _b1.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:_b1];
    [_b1 addTarget:self action:@selector(selectedButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    _b2 = [UIButton buttonWithType:UIButtonTypeCustom];
    _b2.frame = CGRectMake(self.view.mj_w * 0.5, 0, self.view.mj_w * 0.5, 40);
    
    [_b2 setTitle:@"课程选集" forState:UIControlStateNormal];
    [_b2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_b2 setTitleColor:MJRefreshColor(220, 188, 151) forState:UIControlStateSelected];
    _b2.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:_b2];
    [_b2 addTarget:self action:@selector(selectedButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.b1.selected = YES;
    self.b1.tag = 100;
    self.b2.tag = 101;
    
    CGFloat w = [self.b1.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}].width;
    
    self.signView = [[UIView alloc] initWithFrame:CGRectMake(self.view.mj_w * 0.25 - w * 0.5, self.b1.mj_h, w, 1)];
    self.signView.backgroundColor = MJRefreshColor(220, 188, 151);
    [self.view addSubview:self.signView];
    
    
    [self initController];
    [self initShowArea];
}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.showArea.frame = CGRectMake(0, self.b1.mj_h + 1, self.view.mj_w, self.view.mj_h - self.b1.mj_h - 1);
}
- (void)initController {
    _studyVC = [[VC_StudyData alloc] init];
    [_studyVC refreshWithModel:nil CallBackBlock:self.block];
    [self addChildViewController:_studyVC];
    [_studyVC didMoveToParentViewController:self];
    
    _workVC = [[VC_CourseSelectedWorks alloc] init];
    [self addChildViewController:_workVC];
    [_workVC didMoveToParentViewController:self];
}

- (void)initShowArea {
    self.showArea = _studyVC.view;
    self.showArea.frame = CGRectMake(0, self.b1.mj_h + 1, self.view.mj_w, self.view.mj_h - self.b1.mj_h - 1);
    [self.view addSubview:self.showArea];
    _currentViewController = _studyVC;
}

#pragma mark --mark EVENT

- (void)selectedButtonClick:(UIButton *)sender {
    if (sender.isSelected) {
        return;
    }
    self.b1.selected = NO;
    self.b2.selected = NO;
    sender.selected = YES;
    [self selectedWithIndex:sender.tag - 100];
}
#pragma mark --mark 业务逻辑

- (void)selectedWithIndex:(NSInteger)index {
    CGRect frame = self.signView.frame;
    CGFloat w = [self.b1.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}].width;
    UIViewController *vc;
    if (index == 0) {
        frame = CGRectMake(self.view.mj_w * 0.25 - w * 0.5, self.b1.mj_h, w, 1);
        vc = _studyVC;
    }else {
        frame = CGRectMake(self.view.mj_w * 0.75 - w * 0.5, self.b1.mj_h, w, 1);
        vc = _workVC;
    }
    [UIView animateWithDuration:0.1f animations:^{
        self.signView.frame = frame;
    }];
    [self transitionFromViewController:_currentViewController toViewController:vc duration:0.1 options:UIViewAnimationOptionTransitionNone animations:^{
        self.showArea = vc.view;
    } completion:^(BOOL finished) {
        _currentViewController = vc;
    }];
}

@end

